home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / haeberli / objtools / spin.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  4KB  |  226 lines

  1. /*
  2.  * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*
  18.  *    spin -
  19.  *        View an sgi object interactively.
  20.  *
  21.  *            Paul Haeberli - 1990
  22.  */
  23. #include "stdio.h"
  24. #include "gl.h"
  25. #include "device.h"
  26. #include "sgiobj.h"
  27.  
  28. /*
  29.  *    spin support follows
  30.  *
  31.  */
  32. float fgetmousex();
  33. float fgetmousey();
  34. float flerp();
  35.  
  36. sgiobj* obj;
  37. int shownormals;
  38. int showlines;
  39. int showcolors;
  40. int showmeshs;
  41. int menu;
  42. int envmap;
  43.  
  44. main(argc,argv)
  45. int argc;
  46. char **argv;
  47. {
  48.     short val;
  49.     float zfactor;
  50.  
  51.     if(argc<2) {
  52.     fprintf(stderr, "usage: spin object [environ.rgb]\n");
  53.     exit(1);
  54.     }
  55.     obj = readsgiobj(argv[1]);
  56.     if(!obj) {
  57.     fprintf(stderr, "spin: can't read %s\n",argv[1]);
  58.     exit(1);
  59.     }
  60.     zfactor = 5.0;
  61.     initwindow();
  62.     trackztrans(-3.0);
  63.     if(argc>=3) {
  64.     if(getgdesc(GD_TEXTURE)) {
  65.         textureread(argv[2],1);
  66.         envmap = 1;
  67.     } else {
  68.         fprintf(stderr, "spin: no texturing on this machine - oh well\n");
  69.     }
  70.     }
  71.     qmouse();
  72.     qdevice(ESCKEY);
  73.     qdevice(UPARROWKEY);
  74.     qdevice(DOWNARROWKEY);
  75.     while(1) {
  76.     while(qtest()) {
  77.         switch(qread(&val)) {
  78.           case ESCKEY:
  79.                     exit(0);
  80.                     break;
  81.         case LEFTMOUSE:
  82.             if(val) {
  83.             trackclick();
  84.             while(getbutton(LEFTMOUSE)) { 
  85.                 trackpoll();
  86.                 drawit(obj);
  87.             }
  88.             }
  89.             break;
  90.         case MIDDLEMOUSE:
  91.             if(val) {
  92.             trackclick();
  93.             while(getbutton(MIDDLEMOUSE)) {
  94.                 trackpoll();
  95.                 drawit(obj);
  96.             }
  97.             }
  98.             break;
  99.         case MENUBUTTON:
  100.             if(val) {
  101.             switch(dopup(menu)) {
  102.                 case 1:
  103.                 shownormals = 1-shownormals;
  104.                 break;
  105.                 case 2:
  106.                 showlines = 1-showlines;
  107.                 break;
  108.                 case 3:
  109.                 showcolors = 1-showcolors;
  110.                 break;
  111.                 case 4:
  112.                 savewindow("save.rgb");
  113.                 printf("saved image to save.rgb\n");
  114.                 break;
  115.                 case 6:
  116.                 showmeshs = 1-showmeshs;
  117.                 break;
  118.             }
  119.             }
  120.             break;
  121.         case UPARROWKEY:
  122.             if(val) {
  123.             zfactor *= 1.41;
  124.             }
  125.             break;
  126.         case DOWNARROWKEY:
  127.             if(val) {
  128.             zfactor /= 1.41;
  129.             }
  130.             break;
  131.  
  132.         }
  133.     }
  134.     drawit(obj);
  135.     }
  136. }
  137.  
  138. initwindow()
  139. {
  140.     keepaspect(5,4);
  141.     winopen("spin");
  142.     if(obj->objtype == OBJ_TRIMESH)
  143.     menu = defpup("spin %t|toggle normals|toggle lines|toggle color|save image|save lenticular|toggle showmeshs");
  144.     else
  145.     menu = defpup("spin %t|toggle normals|toggle lines|toggle color|save image|save lenticular");
  146.     matrixinit();
  147.     mygfxinit();
  148.     backface(0);
  149.     renderfperspective(25.0,5.0/4.0,0.3,20.0); 
  150.     shadeinit();
  151. }
  152.  
  153. clearback()
  154. {
  155.     shadeoff();
  156.     zclear();
  157.     cpack(0x000000);
  158.     zbuffer(0);
  159.     clear();
  160. }
  161.  
  162. drawit(obj)
  163. sgiobj *obj;
  164. {
  165.     reshapeviewport();
  166.     clearback();
  167.     pushmatrix();
  168.     tracktransform();
  169.     drawtheobj(obj);
  170.     popmatrix();
  171.     swapbuffers();
  172. }
  173.  
  174. drawtheobj(obj)
  175. sgiobj *obj;
  176. {
  177.     srand(3);
  178.     if(showlines) {
  179.     zbuffer(0);
  180.     cpack(0x40ffffff);
  181.     drawsgiobj(obj,DRAW_LINES);
  182.     } else {
  183.     zbuffer(1);
  184.     if(envmap) {
  185.        cpack(0xffffff);
  186.         texgen(TX_S, TG_SPHEREMAP, 0);
  187.         texgen(TX_T, TG_SPHEREMAP, 0);
  188.         texgen(TX_S, TG_ON, NULL);
  189.         texgen(TX_T, TG_ON, NULL);
  190.         drawsgiobj(obj,DRAW_POINTS|DRAW_NORMALS);
  191.         texgen(TX_S, TG_OFF, NULL);
  192.         texgen(TX_T, TG_OFF, NULL);
  193.     } else {
  194.         shadeon();
  195.         setdiffuse(0.0,0.6,0.0);
  196.         setspecular(0.9,0.9,0.9);
  197.         lmcolor(LMC_DIFFUSE);
  198.         if(showmeshs) 
  199.         drawsgiobjmeshs(obj);
  200.         else if(showcolors)
  201.         drawsgiobj(obj,DRAW_POINTS|DRAW_NORMALS|DRAW_COLORS);
  202.         else 
  203.         drawsgiobj(obj,DRAW_POINTS|DRAW_NORMALS);
  204.         lmcolor(LMC_COLOR);
  205.         shadeoff();
  206.     }
  207.     }
  208.     if(shownormals) {
  209.     cpack(0x0000ff);
  210.     drawsgiobjnorms(obj);
  211.     }
  212. }
  213.  
  214. mygfxinit()
  215. {
  216.     zbuffer(1);
  217.     subpixel(1);
  218.     RGBmode();
  219.     doublebuffer();
  220.     gconfig();
  221.     cpack(0x808080);
  222.     clear();
  223.     swapbuffers();
  224.     fsetdepth(0.0,1.0);
  225. }
  226.